<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Myjob extends Model
{
    protected $table = 'dd_myjob';
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $guarded = array();
    
    public static function updatetick(\Illuminate\Http\Request $request, $id) {
        $mjob = static::find($id);
        if ($mjob->iperid != session('job')->id) {
            return ['status'=>0, 'info'=>'非法操作'];
        }
        if (in_array($mjob->jstatus,[1, 2])) {
            return ['status'=> 0, 'info'=>'非法操作'];
        }
        $mjob->jstatus = $request->input("jstatus");
        $mjob->save();
        if ($request->has('causeid')) {
            $data['causeid'] = $request->input('causeid');
        }
        if ($request->has('suggest')) {
            $data['suggest'] = $request->input('suggest');
        }
        $data['jobid'] = $mjob->jobid;
        $data['iperid'] = $mjob->iperid;
        $data['btime'] = time();
        $data['type'] = $mjob->jstatus;
        Tick::create($data);
        Jobs::where('id', $mjob->jobid)->update(['status'=>($mjob->jstatus+1)]);
        if ($mjob->jstatus == 1)
            return ['status'=> 1, 'info'=>'任务反馈成功'];
        else 
            return ['status'=> 1, 'info'=>'回单成功'];
    }
    
    /**
     * 派单时间
     */
    public function strCtime() {
        return $this->ctime ? date("Y-m-d H:i:s", $this->ctime): "";
    }
    
    /**
     * 客户所在小区
     */
    public function strPlot() {
        $plot = Plot::find($this->plotid);
        return $plot ? $plot->name : "未知";
    }
    /**
     * 任务是否转派
     */
    public function strIsResend() {
        if ($this->status == 0) {
            return "未转派";
        } elseif ($this->status == 1) {
            return "已转派";
        }
    }
    /**
     * 任务状态
     */
    public function strJobStatus() {
        switch ($this->jstatus) {
            case 0:
                $st = "未完成";
                break;
            case 1:
                $st = "已反馈";
                break;
            case 2:
                $st = "已回单";
                break;
            default :
                $st = "未知";
        }
        return $st;
    }
    
}
